package com.yao2san.sim.storage.client.util;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class StorageUtil {

    private final static String[] SPECIAL_CHARS = {"../", "./", ".\\", ".\\\\"};

    public static void checkObjectName(String... objects) {
        if (objects == null) {
            return;
        }
        for (String object : objects) {
            if (object != null) {
                for (String specialChar : SPECIAL_CHARS) {
                    if (object.contains(specialChar)) {
                        throw new IllegalArgumentException("Illegal parameter:" + specialChar);
                    }
                }
            }
        }
    }
    public static boolean isWindows(){
        return System.getProperty("os.name").toLowerCase().contains("windows");
    }

    public static String formatPath(String path) {
        return path.replaceAll("(/+|\\\\+)+", "/");
    }

    public static String md5(String string) {
        MessageDigest md5;
        try {
            md5 = MessageDigest.getInstance("MD5");
            byte[] bytes = md5.digest(string.getBytes(StandardCharsets.UTF_8));
            StringBuilder sb = new StringBuilder();
            for (byte b : bytes) {
                sb.append(String.format("%02x", b));
            }
            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }
}
